{createAction} = require('redux-actions')
tFetch = require('../utilities/tFetch.js')

actions =
  getListStart:createAction('YHYGF/GET_LIST_START')
  getListCompleted:createAction('YHYGF/GET_LIST_COMPLETED')
  refreshListStart:createAction('YHYGF/REFRESH_LIST_START')
  refreshListCompleted:createAction('YHYGF/REFRESH_LIST_COMPLETED')

  fetchRefreshList:(startDate,endDate,token)->
    (dispatch)->
      dispatch actions.refreshListStart()
      str = "http://202.136.214.173/api/T_REPORT_MARKCOMPUTE?timestart=#{startDate}&timeend=#{endDate}&dc=00&pageNum=1"
      promise = fetch(str,{
                  method: 'GET'
                  mode: 'cors'
                  cache: 'default'
                  headers:
                    'Content-Type':'application/x-www-form-urlencoded'
                    'token':token
                })
      tFetch dispatch,promise,(json)->
        date = new Date()
        dispatch actions.refreshListCompleted
          obj:json
          lastFetchTime:date


      # str = "http://202.136.214.173/api/T_REPORT_MARKCOMPUTE?timestart=#{startDate}&timeend=#{endDate}&dc=00&pageNum=1"
      # fetch(str,{
      #     method: 'GET'
      #     mode: 'cors'
      #     cache: 'default'
      #     headers:
      #       'Content-Type':'application/x-www-form-urlencoded'
      #       'token':token
      #   })
      # .then (res)->
      #   res.json()
      # .then (json)->
      #   date = new Date()
      #   dispatch actions.refreshListCompleted
      #     obj:json
      #     lastFetchTime:date
      # .catch (err)->
      #   console.log(err)


  fetchGetList:(pageNum,startDate,endDate,token)->
    (dispatch)->
      debugger
      dispatch actions.getListStart()
      str = "http://202.136.214.173/api/T_REPORT_MARKCOMPUTE?timestart=#{startDate}&timeend=#{endDate}&dc=00&pageNum=#{pageNum}"
      promise = fetch(str,{
                  method: 'GET'
                  mode: 'cors'
                  cache: 'default'
                  headers:
                    'Content-Type':'application/x-www-form-urlencoded'
                    'token':token
                })
      tFetch dispatch,promise,(json)->
        date = new Date()
        dispatch actions.getListCompleted
          obj:json
          lastFetchTime:date
      # str = "http://202.136.214.173/api/T_REPORT_MARKCOMPUTE?timestart=#{startDate}&timeend=#{endDate}&dc=00&pageNum=#{pageNum}"
      # fetch(str,{
      #     method: 'GET'
      #     mode: 'cors'
      #     cache: 'default'
      #     headers:
      #       'Content-Type':'application/x-www-form-urlencoded'
      #       'token':token
      #   })
      # .then (res)->
      #   res.json()
      # .then (json)->
      #   date = new Date()
      #   dispatch actions.getListCompleted
      #     obj:json
      #     lastFetchTime:date
      # .catch (err)->
      #   console.log(err)

module.exports = actions
